{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# Reference: https://jupyterbook.org/interactive/hiding.html\n",
    "# Use {hide, remove}-{input, output, cell} tags to hiding content\n",
    "\n",
    "import sys\n",
    "import os\n",
    "if not any(path.endswith('textbook') for path in sys.path):\n",
    "    sys.path.append(os.path.abspath('../../..'))\n",
    "from textbook_utils import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercises\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+  Here we walk you through a test of the hypothesis related to the zeros observed in the two groups. Specifically, we examine the difference in the proportion of zeros in the groups: $ \\hat{p}_{T} - \\hat{p}_C,$ where $\\hat{p}_T = \\# 0s \\textrm{ in treatment} / 100$, and $\\hat{p}_C$ is similarly defined for the control group. \n",
    "    + What is the Null Hypothesis? \n",
    "    + Describe an urn that reflects the null. Include in your description, the number of draws from the urn, whether draws are with or without replacement, and the statistic you plan to compute.\n",
    "    + Carry out the simulation with 10,000 trials. Recall from {numref}`Chapter %s <ch:theory_datadesign>` that there is a named probability distribution that you can use to carry out your simulation. \n",
    "    + Plot the approximate sampling distribution for the statistic.\n",
    "    + Compute the approximate the $p$-value, which is the chance of seeing a difference at least as big as your observed statistic. \n",
    "    + Describe your conclusions from this hypothesis test. Is the probability a surprise? \n",
    "   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ Let's again walk through a hypothesis test for the Wikipedia experiment. This time though, the test is based on a comparison of medians, and is an example of a randomization test. The test works conditionally on the observed data, and considers all possible alternative outcomes based on a different, random assignment. \n",
    "    + The test statistic is the difference: $median_T - median_C$. \n",
    "    + What is the Null Hypothesis? This time, our model doesn't reduce to an urn of marbles labeled zero or one. \n",
    "    + Describe an urn that reflects the null. Include in your description, the number of draws from the urn, whether draws are with or without replacement, and the statistic you plan to compute.\n",
    "    + Carry out the simulation with 10,000 trials. Simulating this sampling distribution was not practical before today's world of cheap, powerful computers. \n",
    "        + To carry out this simulation, set up the urn as an array of productivity data, where the first 100 values are the productivity values from the treatment and the next 100 are from the control group.\n",
    "        + Shuffle the 200 values in the array. Then the first 100 values will represent a randomly sampled treatment group and the next 100 the control. We write a function to find the difference in medians.\n",
    "        + Shuffle the marbles in the urn 10000 times, and compute the difference in medians for each shuffle.     \n",
    "    + Plot the approximate sampling distribution for the statistic.\n",
    "    + Compute the approximate the $p$-value, which is the chance of seeing a difference at least as big as your observed statistic. \n",
    "    + Describe your conclusions from this hypothesis test. Is the probability a surprise? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#sim_right_P = np.random.poisson(lam=n_good_C, size=500000)\n",
    "#sim_left_P = np.random.poisson(lam=n_good_T, size=500000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ We saw earlier that the two groups had quite a bit different median productivity. Under the null, we expect the treatment to have no effect and each contributor's productivity to be unaffected by their receipt of the award. That is, the productivity for a top contributor would be unchanged by the receipt of an informal award. Any difference in the observed medians for the two groups is simply due to the chance assignment to treatment and control.  \n",
    "\n",
    "Now our statistic is the difference: $median_T - median_C$. This time, our model doesn't reduce to an urn of marbles labeled zero or one. Instead, it is an urn with 200 marbles, each labeled with the productivity of one of the contributors. Again, we draw 100 marbles from the urn to find the treatment group, and the marbles remaining in the urn represent the control group. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#urn = wiki[['postproductivity']].to_numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our statistic is the difference: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#sample_median_dif = np.median(urn[100:]) - np.median(urn[:100])\n",
    "#sample_median_dif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we might expect, the sampling distribution of the difference in medians is centered on 0. This reflects our assumptions of the treatment having no effect. Our observed statistic is quite large (in the negative sense). We use this simulated sampling distribution to find the approximate $p$-value for observing a difference in medians at least as big as ours."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#len([elem for elem in median_diff_simulation if elem <= -212]) / 10000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This $p$-value is an even bigger surprise than the $p$-value based on the proportion of zeros in the groups. Only about 3 of the 10,000 simulated samples had a difference this large. This test raises doubts about the null model. Statistical logic has us conclude that the pattern is real. How do we interpret this? The experiment was well designed. The 200 contributors were selected at random from the top 1% and then they were divided at random into two groups. These chance processes say that we can rely on the sample of 200 being representative of top contributors, and the treatment and control groups being similar to each other in every way except for the application of the treatment. Given the careful design, we conclude that informal awards have a positive effect on productivity for top contributors.\n",
    "\n",
    "Such a simulation like the one we just performed, was not practical before today's world of cheap, powerful computers.  An alternative approach, that was developed in the 1950s and 1960s, uses rank statistics. The approach is still used today with highly skewed samples and multiple tests. We describe it next."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We wrap up this section by noting that the sampling distributions for all three tests are roughly symmetric and normal in shape. But, the sampling distribution for the rank-sum statistic is nearly perfect in its normal shape. This is because the ranks maintain order, but remove skewness from the data, so the sum of random ranks has an approximate normal distribution for sums of ranks for samples of as few as 20 to 30 values. While, we have simply simulated the approximate sampling distribution of the rank-sum and not used this normal property of the sampling distribution, it was originally designed as a statistics because of this useful property, and today, when conducting hundreds and thousands of A/B tests, many of which have skewed empirical distributions, tests based on ranks are still popular."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simulations we carried out in this section, were possible because our null model completely specified the chance mechanism for the data. That is often not the case, and the next section introduces an approach to approximate the model using the data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ We encountered a hypothesis test in {numref}`Chapter %s <ch:theory_datadesign>` without actually calling it that. Recall the randomized clinical trial of the J\\&J vaccine. There were 43,738 people enrolled in the trial, randomly split in equal groups. The group that received the vaccine had 117 sick people, and the control group had 351. \n",
    "\n",
    "*Statistic* The statistic used is vaccine efficacy, \n",
    "\n",
    "$$ \\hat{VE} =  \\frac {\\hat{p}_{C} - \\hat{p}_T} {\\hat{p}_T} $$\n",
    "\n",
    "And the value of vaccine efficacy in this trial was,\n",
    "\n",
    "$$ \\frac{(351/21869 - 117/21869)} {351/21869} \n",
    "   = \\frac{351 - 117}{351} = 0.67$$\n",
    "\n",
    "\n",
    "*Model* The CDC set a standard of 50\\% for $VE$, meaning that the efficacy has to exceed 50\\% to be approved for distribution.  The model assumes that the vaccine has *no* effect on health and any difference observed between the treatment and control groups is due to the chance process in assigning people to groups. We set the Null hypothesis as the status quo that the vaccine doesn't work; we hope to find a surprise and reject the Null. \n",
    "\n",
    "In this first test our statistic relates to the zeros observed in the two groups. Specifically, we examine the difference in the proportion of zeros in the groups. \n",
    "\n",
    "$$ \\hat{p}_{T} - \\hat{p}_C,$$\n",
    "\n",
    "where $\\hat{p}_T = \\# 0s \\textrm{ in treatment} / 100$, and $\\hat{p}_C$ is similarly defined for the control group. The Null Hypothesis assumes that this difference is 0, and it's simply the luck of the draw that resulted in so few zeros in the treatment group.   \n",
    " \n",
    "\n",
    "Under the null, the proportion of zeros in the treatment and control groups is the same, and any observed difference is due to the random variation in assignment to treatment/control group. In other word, the 19 contributors who had no productivity in the 90-day period following the treatment, would have behaved exactly the same whether they were assigned to treatment or control. We can represent this null model as an urn with 200 marbles, with 19 of them marked $0$ and $181$ marked with a $1$, and we draw 100 times to select the treatment group. In {numref}`Chapter %s <ch:theory_datadesign>`, we encountered a similar situation with a vaccine trial. There we found that draws from a 0-1 urn followed a named distribution called the hypergeometric, and `numpy` provides a simulator for this model, making it easy to find the p-value.  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
